package session;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import rental.Car;
import rental.CarType;
import rental.Reservation;
import rental.ReservationException;

/**
 *
 * @author wouterdb
 */
@Stateless
public class ManagerSession implements ManagerSessionRemote {

    @PersistenceUnit
    private EntityManagerFactory emf;
    private EntityManager em;

    public ManagerSession(){
        em = emf.createEntityManager();
    }
    
    @Override
    public Set<CarType> getCarTypes(String company) {
        return new HashSet<CarType>(); // TODO
    }

    @Override
    public Set<Integer> getCars(String company, String type) {
        String qry = "SELECT c.CAR_ID FROM Car car INNER JOIN Company comp " +
                " WHERE comp.name = :company AND car.type.name = :carType";
        TypedQuery tq = em.createNamedQuery(qry,Integer.class);
        tq.setParameter("company", company);
        tq.setParameter("carType", type);
        return new HashSet<Integer>(tq.getResultList());
    }

    @Override
    public Set<Reservation> getReservations(String company, String type, int id) {
        Reservation res = new Reservation();
        String qry = "SELECT c.CAR_ID FROM Reservation res INNER JOIN Company comp " +
                " WHERE res.rentalCompany = :company AND res.carId = :carId AND res.carType = :carType";
        TypedQuery tq = em.createNamedQuery(qry,Reservation.class);
        tq.setParameter("company", company);
        tq.setParameter("carType", type);
        tq.setParameter("carId", id);
        return new HashSet<Reservation>(tq.getResultList());
    }

    @Override
    public Set<Reservation> getReservations(String company, String type) {
        String qry = "SELECT c.CAR_ID FROM Reservation res INNER JOIN Company comp " +
                " WHERE res.rentalCompany = :company AND res.carType = :carType";
        TypedQuery tq = em.createNamedQuery(qry,Reservation.class);
        tq.setParameter("company", company);
        tq.setParameter("carType", type);
        return new HashSet<Reservation>(tq.getResultList());
    }
  
}
